﻿<common:LayoutAwarePage
    x:Name="pageRoot"
    x:Class="SuperQuiz.QuizPage"
    DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
    IsTabStop="false"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:SuperQuiz"
    xmlns:utils="using:SuperQuiz.Utils"
    xmlns:logic="using:SuperQuiz.Logic"
    xmlns:common="using:SuperQuiz.Common"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Page.Resources>
        <utils:StringToImageConverter x:Key="StringToImageConverter"/>
        <CollectionViewSource
            x:Name="itemsViewSource"
            Source="{Binding Category.Quizzes}"
            d:Source="{Binding Categories[0].Quizzes, Source={d:DesignInstance Type=logic:QuizApplication, IsDesignTimeCreatable=True}}" />
    </Page.Resources>
    <common:LayoutAwarePage.BottomAppBar>
        <AppBar IsSticky="True" IsOpen="True">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <StackPanel Orientation="Horizontal"/>
                <StackPanel Grid.Column="1" HorizontalAlignment="Right" Orientation="Horizontal">
                    <Button x:Uid="AppBarPlay" AutomationProperties.Name="Play" Style="{StaticResource PlayAppBarButtonStyle}" Click="PlayClick"/>
                </StackPanel>
            </Grid>
        </AppBar>
    </common:LayoutAwarePage.BottomAppBar>

    <!--
        This grid acts as a root panel for the page that defines two rows:
        * Row 0 contains the back button and page title
        * Row 1 contains the rest of the page layout
    -->
    <Grid
        Style="{StaticResource LayoutRootStyle}"
        DataContext="{Binding Category}"
        d:DataContext="{Binding Categories[0], Source={d:DesignInstance Type=logic:QuizApplication, IsDesignTimeCreatable=True}}">

        <Grid.RowDefinitions>
            <RowDefinition Height="140" x:Name="headerRow"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!-- Back button and page title -->
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Button x:Name="backButton" Click="GoBack" IsEnabled="{Binding Frame.CanGoBack, ElementName=pageRoot}" Style="{StaticResource BackButtonStyle}"/>
            <TextBlock x:Name="pageTitle" Text="{Binding Name}" Style="{StaticResource PageHeaderTextStyle}" Grid.Column="1"/>
        </Grid>

        <!--
            The remainder of the page is one large FlipView that displays details for
            one item at a time, allowing the user to flip through all items in the chosen
            group
        -->
        <FlipView
            x:Name="flipView"
            AutomationProperties.AutomationId="ItemsFlipView"
            AutomationProperties.Name="Item Details"
            TabIndex="1"
            Grid.Row="1"
            Margin="0,-3,0,0"
            ItemsSource="{Binding Source={StaticResource itemsViewSource}}">

            <FlipView.ItemTemplate>
                <DataTemplate>

                    <!--
                        UserControl chosen as the templated item because it supports visual state management
                        Loaded/unloaded events explicitly subscribe to view state updates from the page
                    -->
                    <UserControl Loaded="StartLayoutUpdates" Unloaded="StopLayoutUpdates">
                        <ScrollViewer x:Name="quizDetail" Margin="100,0,100,88" Style="{StaticResource VerticalScrollViewerStyle}" VerticalScrollBarVisibility="Hidden">
                            <Grid Margin="17,0,17,0">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="Auto" />
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition />
                                    <ColumnDefinition x:Name="detailColumn2" />
                                </Grid.ColumnDefinitions>
                                <TextBlock Style="{StaticResource SubheaderTextStyle}" Text="{Binding Name}" Grid.ColumnSpan="2"/>
                                <Image x:Name="image" MaxHeight="480" Margin="0,20,4,10" Stretch="Uniform" Source="{Binding ImageUri, Converter={StaticResource StringToImageConverter}}" Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Top"/>
                                <ScrollViewer x:Name="scrollViewerDescription" Style="{StaticResource VerticalScrollViewerStyle}" Grid.Column="1" Grid.Row="1" >
                                    <TextBlock Style="{StaticResource LargeTextBlockTextStyle}" Text="{Binding Description}" Margin="4,0"/>
                                </ScrollViewer>
                            </Grid>
                            <VisualStateManager.VisualStateGroups>

                                <!-- Visual states reflect the application's view state inside the FlipView -->
                                <VisualStateGroup x:Name="ApplicationViewStates">
                                    <VisualState x:Name="FullScreenLandscape"/>
                                    <VisualState x:Name="Filled"/>

                                    <!-- Respect the narrower 100-pixel margin convention for portrait -->
                                    <VisualState x:Name="FullScreenPortrait">
                                    </VisualState>

                                    <!-- When snapped, the content is reformatted and scrolls vertically -->
                                    <VisualState x:Name="Snapped">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="quizDetail" Storyboard.TargetProperty="Margin">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="0,0,0,88"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="detailColumn2" Storyboard.TargetProperty="Width">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="0"/>
                                            </ObjectAnimationUsingKeyFrames><ObjectAnimationUsingKeyFrames Storyboard.TargetName="scrollViewerDescription" Storyboard.TargetProperty="(Grid.Column)">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="0"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="quizDetail" Storyboard.TargetProperty="VerticalScrollBarVisibility">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Auto"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="scrollViewerDescription" Storyboard.TargetProperty="(Grid.Row)">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="2"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="image" Storyboard.TargetProperty="MaxHeight">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="160"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                        </ScrollViewer>
                    </UserControl>
                </DataTemplate>
            </FlipView.ItemTemplate>
        </FlipView>

        <VisualStateManager.VisualStateGroups>

            <!-- Visual states reflect the application's view state -->
            <VisualStateGroup x:Name="ApplicationViewStates">
                <VisualState x:Name="FullScreenLandscape"/>
                <VisualState x:Name="Filled"/>

                <!-- The back button respects the narrower 100-pixel margin convention for portrait -->
                <VisualState x:Name="FullScreenPortrait">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PortraitBackButtonStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>

                <!-- The back button and title have different styles when snapped -->
                <VisualState x:Name="Snapped">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="headerRow" Storyboard.TargetProperty="Height">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="80"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedBackButtonStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="pageTitle" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedPageHeaderTextStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </Grid>
</common:LayoutAwarePage>
